package com.ztgf.shopping.service.epg;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ztgf.shopping.common.config.YmlConfig;
import com.ztgf.shopping.mapper.epg.EpgBannerMapper;
import com.ztgf.shopping.models.bo.epg.EpgBannerBO;
import com.ztgf.shopping.models.bo.epg.EpgBannerSaveBO;
import com.ztgf.shopping.models.constant.ShoppingConstant;
import com.ztgf.shopping.models.dto.epg.EpgBannerDTO;
import com.ztgf.shopping.models.enums.ShoppingCodeEnum;
import com.ztgf.shopping.models.service.epg.IEpgBannerService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Banner实现类
 *
 * @author zhanghualang
 * @Date 2020-05-18
 */
@DubboService
public class EpgBannerServiceImpl extends ServiceImpl<EpgBannerMapper, EpgBannerDTO> implements IEpgBannerService {

    @Autowired
    private EpgBannerMapper epgBannerMapper;

    @Resource
    private YmlConfig ymlConfig;

    @Override
    public boolean add(EpgBannerSaveBO epgBannerSaveBO) {
        EpgBannerDTO epgBannerDTO = new EpgBannerDTO();
        BeanUtils.copyProperties(epgBannerSaveBO,epgBannerDTO);
        Long id = ymlConfig.getPkId();
        epgBannerDTO.setId(id);
        return this.insert(epgBannerDTO);
    }

    /**
     * 删除功能功能相关的banner
     * @return
     */
    public boolean deleteByModuleId(Long moduleId){
        Map<String, Object> columnMap = new HashMap<>();
        columnMap.put("module_id",moduleId);
        return epgBannerMapper.deleteByMap(columnMap) > 0;
    }

    /**
     * 根据相关信息查找 banner 有页码
     * @param epgBannerBO
     * @return
     */
    @Override
    public Page<EpgBannerDTO> getBySelectBannerInfoPage(EpgBannerBO epgBannerBO) {
        epgBannerBO = addMustData(epgBannerBO);
        Wrapper<EpgBannerDTO> wrapper = getBySelectBannerInfo(epgBannerBO);
        return selectPage(new Page<>(epgBannerBO.getPageNo(),epgBannerBO.getPageSize()), wrapper);
    }

    /**
     * 添加必要数据
     * @param epgBannerBO
     * @return
     */
    private EpgBannerBO addMustData(EpgBannerBO epgBannerBO){
        epgBannerBO.setIsPublish(ShoppingCodeEnum.PUBLIC_1.getCode());
        epgBannerBO.setStatus(ShoppingCodeEnum.STATUS_0.getCode());
        epgBannerBO.setSort(ShoppingCodeEnum.SORT_ASC.getCode());
        return epgBannerBO;
    }

    /**
     * 根据相关信息查找 banner 无页码
     * @param epgBannerBO
     * @return
     */
    @Override
    public List<EpgBannerDTO> getBySelectBannerInfoNoPage(EpgBannerBO epgBannerBO) {
        epgBannerBO = addMustData(epgBannerBO);
        Wrapper<EpgBannerDTO> wrapper = getBySelectBannerInfo(epgBannerBO);
        if(epgBannerBO.getPageNo() == 0 && epgBannerBO.getPageSize() == 0)
            return epgBannerMapper.selectPage( new Page<>(0, ShoppingConstant.PLATFORM_BANNER_NUM),wrapper);
        return selectList(wrapper);
    }

    private Wrapper<EpgBannerDTO> getBySelectBannerInfo(EpgBannerBO epgBannerBO) {
        Wrapper<EpgBannerDTO> wrapper = new EntityWrapper<>();
        EpgBannerDTO epgBannerDTO = new EpgBannerDTO();
        BeanUtils.copyProperties(epgBannerBO,epgBannerDTO);
        wrapper.eq("status", epgBannerBO.getStatus());
        wrapper.eq("is_publish", epgBannerBO.getIsPublish());
        wrapper.eq("module_id", epgBannerBO.getModuleId());
        if(ShoppingCodeEnum.SORT_DESC.getCode() == epgBannerBO.getSort())//降序
            wrapper.orderBy("sort", false);
        else
            wrapper.orderBy("sort", true);
        return wrapper;
    }
}